// System headers
#include "hw_types.h"
#include "timer.h"
#include "gpio.h"
#include "interrupt.h"
#include "hw_memmap.h"
#include "osram96x16x1.h"
//custom headers			  
#include "interr.h"

// Standard library headers
#include <stdlib.h>

// Global variables
extern signed char flag;  //used after a click for filtration and detection of double click
extern unsigned int seconds; //used to 
extern char equation[30];	  // used to store the whole equasion
extern unsigned char state;	//used by other functions to check for a click or double click
extern char coeffA[10] ;	 //used to store coefficient A
extern char coeffB[10] ;	 //used to store coefficient B
extern char coeffC[10] ;	 //used to store coefficient C

//
//	This function is the interrupt handler for TIMER 0.
//	Sets the global variable "state" with the module of "flag"
//
void Timer0IntHandler(void)
{
	TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
	
	state=abs(flag);
	flag=0;
}
//
// This function is the interrupt handler for TIMER 1.
//Changes the sign of the global variable "flag".
//
void
Timer1IntHandler(void)
{
   	TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);

	if(flag == -1)
		flag=1;
	else if (flag==-2)
		flag=2;
}
//
//This function is the interrupt handler for TIMER 2. 
//It  increases the global variable "seconds" with 1 ,until "seconds" reaches 30.
//
void
Timer2IntHandler(void)
{ 
	TimerIntClear(TIMER2_BASE, TIMER_TIMA_TIMEOUT);
	
	if (seconds < 30)
		seconds++;
	else
	{
		TimerDisable(TIMER2_BASE, TIMER_A);	
		OSRAM96x16x1Clear();
		buildEquation(coeffA, coeffB, coeffC);
		moveEquation(equation);
	    
		TimerEnable(TIMER2_BASE, TIMER_A);	
	}
}
//
//This function is the interrupt handler for the GPIO Port C Pin 4, 
//it starts TIMER0 and TIMER1, and changes the variable "flag" to -1 or -2.
//
void GPIO_C_Int(void)
{
	GPIOPinIntClear(GPIO_PORTC_BASE, PUSH_BUTTON);
	
	if(flag==0)
	{
	
		seconds = 0;
		TimerEnable(TIMER1_BASE, TIMER_A);
		TimerEnable(TIMER0_BASE, TIMER_A);
		TimerEnable(TIMER2_BASE, TIMER_A);
	   	flag=-1;
	}	
 	else if(flag==1)
	{
		seconds = 0;
		TimerEnable(TIMER2_BASE, TIMER_A);
		TimerEnable(TIMER1_BASE, TIMER_A);
		flag=-2;
	}
}
